local skill = fk.CreateSkill {
  name = "hanqing__armchair_strategist_skill",
}

Fk:loadTranslationTable{
  ["hanqing__armchair_strategist_skill"] = "纸上谈兵",
  ["#hanqing__armchair_strategist-use"] = "你可以将【纸上谈兵】当本回合使用过的一张基本牌或普通锦囊牌使用",
}

skill:addEffect("cardskill", {
  can_use = Util.FalseFunc,
  interaction = function(self, player)
    local all_names = player:getMark("hanqing__armchair_strategist-tmp")
    return UI.CardNameBox {
      choices = player:getViewAsCardNames(self.name, all_names),
      all_choices = all_names,
    }
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).name == "hanqing__armchair_strategist"
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    if #selected_cards == 1 and self.interaction.data then
      local c = Fk:cloneCard(self.interaction.data)
      c:addSubcard(selected_cards[1])
      return c.skill:targetFilter(player, to_select, selected, {}, c, { bypass_times = true })
    end
  end,
  feasible = function (self, player, selected, selected_cards)
    if #selected_cards == 1 and self.interaction.data then
      local c = Fk:cloneCard(self.interaction.data)
      c:addSubcard(selected_cards[1])
      return c.skill:feasible(player, selected, {}, c)
    end
  end,
})

skill:addEffect(fk.TurnEnd, {
  global = true,
  mute = true,
  priority = 0.001,
  can_trigger = function(self, event, target, player, data)
    if target == player and
      table.find(player:getHandlyIds(), function(id)
        return Fk:getCardById(id).name == "hanqing__armchair_strategist"
      end) then
      local names = {}
      player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data
        if use.card.type == Card.TypeBasic or use.card:isCommonTrick() then
          table.insertIfNeed(names, use.card.name)
        end
      end, Player.HistoryTurn)
      if #names > 0 then
        event:setCostData(self, {choices = names})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = event:getCostData(self).choices,
      skill_name = skill.name,
      prompt = "#hanqing__armchair_strategist-use",
      cancelable = true,
      extra_data = {
        bypass_times = true,
        extraUse = true,
      },
      card_filter = {
        n = 1,
        pattern = ".|.|.|.|hanqing__armchair_strategist",
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).extra_data
    room:useCard(use)
    if not player.dead and not use.damageDealt and room:getCardArea(use.card) == Card.DiscardPile then
      room:moveCardTo(use.card, Card.PlayerHand, player, fk.ReasonJustMove, skill.name)
    end
  end,
})

return skill
